package org.jeecg.modules.utils;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import shade.fasterxml.jackson.databind.ObjectMapper;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class TiandituApi {

    private static final String TK = "04260efe0e9c15058d4379e17f842b71"; // 替换为你的密钥
    public static void main(String[] args) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 1. 构建请求参数
            String postStr = URLEncoder.encode(buildPostStr("北京大学"), "utf-8");

            // 2. 构造请求 URL
            String url = "http://api.tianditu.gov.cn/v2/search?postStr=" + postStr + "&type=query&tk=" + TK;

            // 3. 发送请求
            HttpGet request = new HttpGet(url);
            String responseBody = httpClient.execute(request, response -> {
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode != 200) {
                    throw new RuntimeException("HTTP请求失败，状态码：" + statusCode);
                }
                return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
            });

            // 4. 解析 JSON 响应
            ObjectMapper mapper = new ObjectMapper();
            ResponseData data = mapper.readValue(responseBody, ResponseData.class);

            // 5. 验证返回状态
            if ("1000".equals(data.status.getInfocode())) {
                List<ResponseData.Poi> pois = data.pois;
                for (ResponseData.Poi poi : pois) {
                    System.out.println("名称: " + poi.getName());
                    System.out.println("地址: " + poi.getAddress());
                    System.out.println("经纬度: " + poi.getLonlat());
                }
            } else {
                System.err.println("API 返回错误：infocode=" + data.status.getInfocode());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 构建 postStr 参数
    private static String buildPostStr(String keyword) {
        return "{"
                + "\"keyWord\":\"" + keyword + "\","
                + "\"level\":12,"
                + "\"mapBound\":\"73.6,3.86,135.05,53.55\","
                + "\"queryType\":7,"
                + "\"start\":0,"
                + "\"count\":10"
                + "}";
    }

    // 响应数据模型
    static class ResponseData {
        Status status;
        List<Poi> pois;

        static class Status {
            String infocode;

            public String getInfocode() {
                return infocode;
            }
        }

        static class Poi {
            String name;
            String address;
            String lonlat;

            public String getName() {
                return name;
            }

            public String getAddress() {
                return address;
            }

            public String getLonlat() {
                return lonlat;
            }
        }
    }

}
